//
//  ZLCurveProgressView.m
//  Drawing
//
//  Created by 中矿科技有限公司 on 17/10/13.
//  Copyright © 2017年 中矿科技有限公司. All rights reserved.
//

#import "ZLCurveProgressView.h"
@interface ZLCurveProgressView ()



@end

@implementation ZLCurveProgressView
{
    CGFloat shapeWither;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
       [self _commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
       [self _commonInit];
    
    }
    return self;
}



- (instancetype)init{
    self = [super init];
    if (self) {
        [self _commonInit];
    }
    return self;
}

-(void)_commonInit{
    
    self.backgroundColor = [UIColor clearColor];
    [self setProgress:0];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, _zlWhith, _zlWhith) cornerRadius:8];
    _shapeLayer = [CAShapeLayer layer]; //得到layer
   // _shapeLayer.strokeColor = [UIColor redColor].CGColor; //线的颜色
    _shapeLayer.fillColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.3].CGColor; //填充颜色
    //_shapeLayer.lineWidth = 2; //线宽
    //连接类型
    _shapeLayer.lineJoin = kCALineCapRound;
    _shapeLayer.lineCap = kCALineCapRound;
    //路径添加到shapeLayer
    
    _shapeLayer.path = path.CGPath;
    [self.layer addSublayer:_shapeLayer];
    
    // _indicateLayer = nil;
    _indicateLayer = [CAShapeLayer layer];
    UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0,0 , _zlHeight) cornerRadius:0];
    
    _indicateLayer.path = path2.CGPath;
    _indicateLayer.fillColor = [UIColor redColor].CGColor;
    [self.layer addSublayer:_indicateLayer];
    

    
    //百分比标签
    UILabel *cLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _zlWhith, _zlHeight)];
    cLabel.text = self.showText;
    cLabel.textColor =[UIColor whiteColor];
    cLabel.textAlignment = NSTextAlignmentCenter;
    cLabel.font =[UIFont boldSystemFontOfSize:14];
    if(!_cLabel)[self addSubview:cLabel];
    self.cLabel = cLabel;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.layer.cornerRadius = _zlRadius;
    self.layer.masksToBounds = YES;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width, _zlWhith) cornerRadius:_zlRadius];
    _shapeLayer.path = path.CGPath;
    self.cLabel.frame =self.bounds;
    
    CGFloat width = (_progress)*self.frame.size.width;
    UIBezierPath *path1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0,width , _zlHeight) cornerRadius:0];
    _indicateLayer.path = path1.CGPath;
}

- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    
     _cLabel.text = [NSString stringWithFormat:@"完成率: %.2f%%", progress * 100];
//Floor(x)，其功能是“向下取整”，
    CGFloat width = (progress)*_zlWhith;
    [_indicateLayer removeFromSuperlayer];

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0,width , _zlHeight) cornerRadius:0];
    
    _indicateLayer.path = path.CGPath;
    _indicateLayer.fillColor = [UIColor redColor].CGColor;
    [self.layer insertSublayer:_indicateLayer atIndex:1];

    
  // [self setNeedsDisplay];
}
-(void)setProgressColor:(UIColor *)progressColor{
    _indicateLayer.fillColor = progressColor.CGColor;
   // [self setNeedsLayout]; //etNeedsDisplay方便绘图，而layoutSubViews方便出来数据
}

//- (void)drawRect:(CGRect)rect
//{
//
//    
//    //1.获得图形上下文
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    //2.绘制四边形
//    CGContextAddRect(context, CGRectMake(0, 0, rect.size.width *self.progress, rect.size.height));
//    //设置颜色
//    [[UIColor purpleColor] setFill];
//    //3.显示在view上
//    CGContextFillPath(context);
//    
//
//    
//}


@end
